import QtQuick 2.6

import "../../Base/Config"
import "../../Base/UiBase"

import Cpp.Request 1.0 as Request

import "News" as News

Rectangle {
    color: Color.foreground
    GridView {
        id: grid
        anchors.fill: parent
        cellWidth: 180 * Size.ratio
        cellHeight: 180 * Size.ratio
        delegate: Rectangle {
            color: Color.foreground
            width: 180 * Size.ratio
            height: 180 * Size.ratio
            SvgzImage {
                width: 180 * Size.ratio
                height: 180 * Size.ratio
                source: model.iconUrl
                LoaderMouseArea {
                    anchors.fill: parent
                    loader : Loader {
                            id: listLoader
                            parent: indexRoot
                            anchors.fill: parent
                            Component {
                                id:newsIndexComponent
                                News.Index {
                                    groupId: model.id
                                    groupName: model.name
                                    listView.delegate: News.News {
                                        width: parent.width
                                        height: 120 * Size.ratio
                                        news: model
                                        mouseArea.onClicked: loader.sourceComponent = newsViewComponent
                                        Loader {
                                            id: loader
                                            parent: indexRoot
                                            anchors.fill: parent
                                            Component {
                                                id: newsViewComponent
                                                News.NewsView {
                                                    news: model
                                                    onVisibleChanged: loader.sourceComponent = null
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    onClicked: {
                        listLoader.sourceComponent = newsIndexComponent
                    }
                }
            }
        }
        model: ListModel {
            id: listModel
        }
    }
    Request.NewsGroupQuery {
        id: newsGroupQuery
        Component.onCompleted: {
            queryAll()
        }
        onOk: {
            listModel.clear()
            for (var i in newsGroups) {
                listModel.append(newsGroups[i])
            }
        }
    }
}
